/*
 * @Descripttion: 
 * @version: 
 * @Author: xcb
 * @Date: 2021-09-03 15:35:08
 * @LastEditors: xcb
 * @LastEditTime: 2021-11-29 15:08:28
 */
import {transform} from "ol/proj"
import proj4 from 'proj4'
import { register } from "ol/proj/proj4";

export function debounce(fn,wait){
    let timeout=null;
    
    wait=wait||600
    return function(){
      let that=this;
      if(timeout!==null) clearTimeout(timeout)
      timeout=setTimeout(()=>{
        fn.apply(that)
      },wait)
    }
  }
  export const flat = function(arr){
    const isDeep=arr.some(item=>item instanceof Array)
    if(!isDeep){
        return arr
    }
    const res = Array.prototype.concat.apply([],arr)
    return flat(res)
}


export function getTrees(list, parentId = 'null', pKey = 'pid', cKey = 'id') {
    let items = {}
    // 获取每个节点的直属子节点，*记住是直属，不是所有子节点
    for (let i = 0; i < list.length; i++) {
      let key = list[i][pKey]
      if (items[key]) {
        items[key].push(list[i])
      } else {
        items[key] = []
        items[key].push(list[i])
      }
    }
    return formatTree(items, parentId, cKey)
  }

  export function getTypeTrees(list, parentId = '2', pKey = 'parentid', cKey = 'id') {
    let items = {}
    // 获取每个节点的直属子节点，*记住是直属，不是所有子节点
    for (let i = 0; i < list.length; i++) {
      let key = list[i][pKey]
      if (items[key]) {
        items[key].push(list[i])
      } else {
        items[key] = []
        items[key].push(list[i])
      }
    }
    return formatTree(items, parentId, cKey)
  }

  // 利用递归格式化每个节点
  function formatTree(items, parentId, cKey) {
    let result = []
    if (!items[parentId]) {
      return result
    }
    for (let t of items[parentId]) {
      const temp = formatTree(items, t[cKey], cKey)
      if (temp.length > 0) {
        t.children = temp
      }
      result.push(t)
    }
    return result
  }
  export function compare(property){
    return function(a,b){
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2;
    }
}
//度转度°分′秒″
export function ToDegrees(val) {
  if (typeof (val) == "undefined" || val == "") {
      return "";
  }
  var i = val.indexOf('.');
  var strDu = i < 0 ? val : val.substring(0, i);//获取度
  var strFen = 0;
  var strMiao = 0;
  if (i > 0) {
       strFen = "0" + val.substring(i);
      strFen = strFen * 60 + "";
      i = strFen.indexOf('.');
      if (i > 0) {
          strMiao = "0" + strFen.substring(i);
          strFen = strFen.substring(0, i);//获取分
          strMiao = strMiao * 60 + "";
          i = strMiao.indexOf('.');
          strMiao = strMiao.substring(0, i + 4);//取到小数点后面三位
          strMiao = parseFloat(strMiao).toFixed(2);//精确小数点后面两位
      }
  }
  return strDu + "°" + strFen + "′" + strMiao+"″";
}

//度°分′秒″转度
export function ToDigital(val, len) {
  let strDu, strFen, strMiao
  if(val.indexOf('°')!=-1){
    strDu=val.split("°")[0]
  }
  if(val.indexOf('′')!=-1){
    strFen=val.split("°")[1].split('′')[0]
  }
  if(val.indexOf('″')!=-1){
    strMiao=val.split("°")[1].split('′')[1].split('″')[0]
  }
  len = (len > 6 || typeof (len) == "undefined") ? 6 : len;//精确到小数点后最多六位   
  strDu = (typeof (strDu) == "undefined" || strDu == "") ? 0 : parseFloat(strDu);
  strFen = (typeof (strFen) == "undefined" || strFen == "") ? 0 : parseFloat(strFen) / 60;
  strMiao = (typeof (strMiao) == "undefined" || strMiao == "") ? 0 : parseFloat(strMiao) / 3600;
  var digital = strDu + strFen + strMiao;
  if (digital == 0) {
      return "";
  } else {
      return digital.toFixed(len);
  }
}
export function unique(arr,val) {
    const res = new Map();
    return arr.filter(item => !res.has(item[val]) && res.set(item[val], 1))
  }
/**
 * 格式化日期  年-月-日  时：分：秒
 * @param {Date} date 
 */
 export  function FormatDate(date) {
  var year = date.getFullYear(); //取得年
  let nowMonth=date.getMonth() + 1
  var month = nowMonth<10?'0'+nowMonth:nowMonth; //取得月,js从0开始取,所以+1
  var day = date.getDate()< 10 ? "0" +date.getDate():date.getDate(); //取得天
  var hour = date.getHours()< 10 ? "0" +date.getHours():date.getHours(); //取得小时
  var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); //取得分钟
  var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); //取得秒
  return year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + second;
}
export function transformevent(targetepsg,transepsg,proj,event){
  proj4.defs(targetepsg,proj);
 
 
  register(proj4);
  return transform([...event],targetepsg,transepsg)
}